package com.wx.cloud.permission.service.impl;

import com.wx.cloud.permission.mapper.PermissionMapper;
import com.wx.cloud.permission.model.Permission;
import com.wx.cloud.permission.model.RolePermission;
import com.wx.cloud.permission.service.PermissionService;
import com.wx.cloud.permission.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> getSelectList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return permissionMapper.getSelectList(data);
    }

    @Override
    public int getSelectCount(Map<String, Object> data) {
        return permissionMapper.getSelectCount(data);
    }

    @Override
    public Permission getPermissionById(Integer id) {
        return permissionMapper.getPermissionById(id);
    }

    @Override
    public String addPermission(Permission permission) {
        permission.setCreateTime(MyDate.getDate());
        permission.setUpdateTime(MyDate.getDate());
        return permissionMapper.addPermission(permission) == 1 ? "success":"false";
    }

    @Override
    public int updatePermission(Permission permission) {
        permission.setUpdateTime(MyDate.getDate());
        return permissionMapper.updatePermission(permission);
    }

    @Override
    public int delPermission(Permission permission) {
        permission.setUpdateTime(MyDate.getDate());
        return permissionMapper.delPermission(permission);
    }

    @Override
    public int updateState(Permission permission) {
        permission.setUpdateTime(MyDate.getDate());
        return permissionMapper.updateState(permission);
    }

    @Override
    public List<RolePermission> getPermissionByRoleId(Integer roleId) {
        return permissionMapper.getPermissionByRoleId(roleId);
    }

    @Override
    public List<RolePermission> getPermissionItemByNoRoleId(Integer roleId) {
        return permissionMapper.getPermissionItemByNoRoleId(roleId);
    }

    @Override
    public String addRolePermission(List<RolePermission> rolePermissions) {
        int result = permissionMapper.addRolePermission(rolePermissions);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public String removeRolePermission(Integer roleId,List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        params.put("roleId",roleId);
        int result = permissionMapper.removeRolePermission(params);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

}
